package com.woniuxy.health.project.web;

import com.woniuxy.health.commons.entity.Package;
import com.woniuxy.health.project.service.PackageService;
import com.woniuxy.health.project.service.RedisService;
import com.woniuxy.health.project.utils.KeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("package")
public class PackageController {
    @Autowired
    private PackageService packageService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisService redisService;

    private String key1="Zset";

    @GetMapping("all")
    public String fingAllPackages(){
        List<Package> packages = packageService.fingAllPackages();
        if (packages.size()>0){

            return "ok";
        }
        return "再看看";
    }

    List<Package> list=new ArrayList<>();

    @GetMapping("allpackages")
    @SuppressWarnings("unchecked")
    public List<Package> findAllPackages() {
        //1.生成key   自己调方法
        String key = KeyUtil.generate(this, "findAllPackages");
        System.out.println(key);
        //2.应该先判断redis中有没有，有直接获取返回
        if (redisTemplate.hasKey(key)) {
            List<Object> objects = redisTemplate.opsForList().range(key, 0, redisTemplate.opsForList().size(key));

            //返回数据
            return (List<Package>) objects.get(0);
        } else {

            List<Package> allPackages = packageService.fingAllPackages();
            list = allPackages;
            //放入redis 设置时间
            redisTemplate.opsForList().leftPushAll(key, allPackages);
            redisTemplate.expire(key, 2000, TimeUnit.SECONDS);

            return allPackages;
        }
    }

    @GetMapping("addpackage")
    public String addHotPackage(int id) {
        Package aPackage = packageService.fingPackageById(id);

        Long rank2 = redisService.rank(key1, aPackage);
        // 如果rank2为null,则缓存里面不存在该值
        if (null == rank2) {
            // 不存在,则添加,默认排序为1
            redisService.zAdd(key1, aPackage, 1.0);
            System.out.println("当前:" + aPackage + ":的搜索次数为" + 1);
            return "默认";
        } else {
            // 如果存在,则获取排序值  并且+1
            int score = (int) redisService.score(key1, aPackage);
            System.out.println("当前:" + aPackage + ":的搜索次数为" + (score + 1));
            redisService.zAdd(key1, aPackage, score + 1);

            return "ok";
        }

    }

        @GetMapping("hotpackages")
        public List<Package> findHotPackages(@RequestBody int id) {
            String key = KeyUtil.generate(this, "findHotPackages");

            //2.应该先判断redis中有没有，有直接获取返回
            if (redisTemplate.hasKey(key)) {


                Set<Object> objects1 = redisService.reverseRange(key1, 0, -1);
                List<Object> list_1 = new ArrayList<>(objects1);


                //返回数据
                return  (List<Package>)list_1.get(0);
            }else {

                List<Package> hotPackages = list;

                //放入redis 设置时间
                redisTemplate.opsForList().leftPushAll(key,hotPackages);
//                redisTemplate.expire(key, 20, TimeUnit.SECONDS);


                return hotPackages;
            }
        }
}
